package ru.spb.nicetu.jdbc;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.swing.table.AbstractTableModel;
import java.sql.*;

/**
 * Created by dchernyshov on 10.03.15.
 */
public class TblModel extends AbstractTableModel {
    private ResultSet result;
    private ResultSetMetaData rsmd;
    private ResultSet resPerson;
    private int fetchSize;
    static private int countForPages;

    public TblModel() {
        fetchSize = 23;
        countForPages = 0;

        Pet pet = null;

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        pet = (Pet) session.get(Pet.class, 1);
        session.getTransaction().commit();
        System.out.println("Pet's name = " + pet.getPetName());

        String url = "jdbc:postgresql://localhost/test_database";
        String user = "test_user";
        String password = "qwerty";
        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            conn.setAutoCommit(false);
            Statement stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            Statement stat2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            stat.setFetchSize(fetchSize);
            result = stat.executeQuery("SELECT * FROM myschema.pets");
            rsmd = result.getMetaData();
            resPerson = stat2.executeQuery("SELECT * FROM myschema.person");
        } catch (SQLException ex) {
            for (Throwable t : ex) {
                t.printStackTrace();
            }
        }
    }

    @Override
    public int getRowCount() {
        /*int rsSize = 0;
        try {
            result.last();
            rsSize = result.getRow();
            result.beforeFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rsSize;*/
        return fetchSize;
    }

    @Override
    public int getColumnCount() {
        int columnlCount = 0;
        try {
            columnlCount = rsmd.getColumnCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return columnlCount;
    }

    @Override
    public String getColumnName(int column) {
        try{
            return rsmd.getColumnName(column + 1);
        }
        catch(SQLException e){
            e.printStackTrace();
            return "";
        }
    }

    @Override
    public Object getValueAt(int i, int i1) {
        try {
            result.absolute(fetchSize*countForPages + i + 1);
            if (i1 == 2) {
                resPerson.absolute(result.getInt("owner"));
                return resPerson.getString("name").replaceAll(" ", "") + " " + resPerson.getString("surname").replaceAll(" ", "");
            } else
                return result.getString(i1 + 1).replaceAll(" ", "");

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int getCountForPages(){
        return countForPages;
    }

    public static void incrementCountForPages(){
        countForPages++;
    }

    public static void decrementCountForPages(){
        if(countForPages > 0)
            countForPages--;
    }
}
